/**
  ******************************************************************************
  * @file    xxx.c 
  * @author  SD Electronics xxx
  * @version V1.0
  * @date    xx-xx-xx
  * @brief   xxxxxxxxxxxxxxxxxx.
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "sd_common.h"

uint16_t user_task_id;
uint16_t fbl_disply_task_id;

struct button_msg_t *button_msg;

const char *button_type_str[] = {
"BUTTON_PRESSED",
"BUTTON_RELEASED",
"BUTTON_SHORT_PRESSED",
"BUTTON_MULTI_PRESSED",
"BUTTON_LONG_PRESSED",
"BUTTON_LONG_PRESSING",
"BUTTON_LONG_RELEASED",
"BUTTON_LONG_LONG_PRESSED",
"BUTTON_LONG_LONG_RELEASED",
"BUTTON_COMB_PRESSED",
"BUTTON_COMB_RELEASED",
"BUTTON_COMB_SHORT_PRESSED",
"BUTTON_COMB_LONG_PRESSED",
"BUTTON_COMB_LONG_PRESSING",
"BUTTON_COMB_LONG_RELEASED",
"BUTTON_COMB_LONG_LONG_PRESSED",
"BUTTON_COMB_LONG_LONG_RELEASED",
};

/** 
  * @brief  app_032FBL_state
  */ 
void (*const app_032FBL_state[2])(uint8_t) =
{0
    
};

/** 
  * @brief  timer_fbl_ctrl_timer
  */ 
os_timer_t fbl_hall_timer;
os_timer_t fbl_CDNDelay_timer;
os_timer_t fbl_beep_timer;
os_timer_t fbl_beepBlink_timer;
os_timer_t fbl_TIME_timer;
os_timer_t fbl_LED_timer;
os_timer_t FBL_TEST_TIMER;

/** 
  * @brief  timer_fbl_ctrl_timer
  */ 
os_timer_t PowerOn_Off_timer;			//开关机定时器
bool PowerOn_Off_flag = false;

/** 
  * @brief  timer_fbl_ctrl_timer
  */ 
ST_POWER_CTRL stPowerCtrl =
{
    &PowerOn_Off_timer,
    500,//ms
    3,  //1s
    false,//未开机
};

/** 
  * @brief  st_FBL_Ctrl
  */ 
ST_FBL_CTRL st_FBL_Ctrl = {
	&stPowerCtrl,

    &fbl_hall_timer,
    &fbl_CDNDelay_timer,
    &fbl_beep_timer,
    &fbl_beepBlink_timer,
    &fbl_TIME_timer,
    &fbl_LED_timer,

    10,//10ms
    100,//100ms
    500,//500ms
    10000,//10s

    0,//蓝牙未连接
    0,//电池电压
    false,//时间继续显示标志
    false,//自动关机标志

    0,//CNT
    0,//时间
    0,//卡路里
    0,//CDN

    EN_DISP_MODE_NULL,  //显示模式
    0,                  //显示数据

    EN_HILINK_CDN_NULL, //Hilink未下发CDN命令
};

/**
  * @brief  fbl_timer_handler
  * @param  None
  * @note	xxx
  * @retval None
  */
static void fbl_test_timer_handler(void *param)
{
    //更新显示
    pmu_set_led2_value(0); //关闭背光
    st_FBL_Ctrl.CNT_Data ++;
    st_FBL_Ctrl.TimeData ++;
    st_FBL_Ctrl.CaloriesData ++;

    if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_CNT)
        st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.CNT_Data;
    else if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_TIME)
        st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.TimeData;
    else if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_CAL)
        st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.CaloriesData;
    else if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_CDN)
        st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.CDN_Data;

    //跳绳显示---可以将这个显示放到另外一个任务里执行
    fbl_display_task_send_event();
}

/**
  * @brief  fbl_timer_handler
  * @param  None
  * @note	xxx
  * @retval None
  */
static void fbl_hall_timer_handler(void *param)
{
	static uint32_t CalroliesCntFlag = 0;
    PST_DEV_MANAGE pstHALL_DEV = SDDevManageRead(EN_DEV_HALL, 0, NULL, NULL);
    {
        if(pstHALL_DEV != NULL)
        {
			PST_HALL_CTRL pstHALLCtrl = (PST_HALL_CTRL)pstHALL_DEV->pvData;
			if(pstHALLCtrl->pstHALLInfo->HALL_CIRCLE > 0)
			{
                pstHALLCtrl->pstHALLInfo->HALL_CIRCLE = 0;
                st_FBL_Ctrl.AutoPowerOffTimeOut = 0;//自动关机标志清零
                 //如果是倒计时模式结束了，不用计数直接退出，并响铃
                if(st_FBL_Ctrl.HilinkCDNModeStart == EN_HILINK_CDN_STOP)
                {
                    //启动定时器打开beep 100ms
                    SDDevManageCtrl(EN_DEV_BEEP, EN_BEEP_ON, 0, NULL);
                    os_timer_start(st_FBL_Ctrl.Beep_timer, st_FBL_Ctrl.Beep_timer_out, false);
                    return;
                }

                //跳绳计数一圈
                st_FBL_Ctrl.CNT_Data ++;
                st_FBL_Ctrl.TimeDisplyContinue = true;
                
                //每跳两圈，卡路里计数一次
                if(CalroliesCntFlag ++ == 1)
                {
                    CalroliesCntFlag = 0;
                    st_FBL_Ctrl.CaloriesData ++;
                }
                //更新显示
                if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_CNT)
                    st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.CNT_Data;
                else if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_TIME)
                    st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.TimeData;
                else if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_CAL)
                    st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.CaloriesData;
                else if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_CDN)
                    st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.CDN_Data;
                //跳绳显示---可以将这个显示放到另外一个任务里执行
                fbl_display_task_send_event();
                //跳绳数据发送到app
                PackDataSend(&st_FBL_Ctrl, EN_FBL_HILINK_PROC_TYPE_CNT);
			}

            
			//printf("SKIP[%d]\r\n", pstHALLCtrl->pstHALLInfo->HALL_CIRCLE);
        }
    }
}


/**
  * @brief  fbl_CDNDealytimer_handler
  * @param  None
  * @note	xxx
  * @retval None
  */
static void fbl_CDNDealytimer_handler(void *param)
{
    st_FBL_Ctrl.HilinkCDNModeStart = EN_HILINK_CDN_START;
    SDDevManageCtrl(EN_DEV_BEEP, EN_BEEP_ON, 0, NULL);
    os_timer_start(st_FBL_Ctrl.Beep_timer, st_FBL_Ctrl.Beep_timer_out, false);
    os_timer_start(st_FBL_Ctrl.HALL_timer, st_FBL_Ctrl.HALL_timer_out, true);
    os_timer_start(st_FBL_Ctrl.TIME_timer, st_FBL_Ctrl.TIME_timer_out, true);
}
/**
  * @brief  fbl_beep_timer_handler
  * @param  None
  * @note	xxx
  * @retval None
  */
static void fbl_beep_timer_handler(void *param)
{
    SDDevManageCtrl(EN_DEV_BEEP, EN_BEEP_OFF, 0, NULL);
}

/**
  * @brief  fbl_beepBlink_timer_handler
  * @param  None
  * @note	xxx
  * @retval None
  */
static void fbl_beepBlink_timer_handler(void *param)
{
    static int TimeCount = 0;
    static uint8_t BEEPON = 0;
    static uint8_t BeepBlinkCount = 0;
    if(BEEPON == 0)
    {
        SDDevManageCtrl(EN_DEV_BEEP, EN_BEEP_ON, 0, NULL);
        BEEPON = 1;
    }
    else if(BEEPON == 1)
    {
        if(TimeCount ++ == 10)
        {
            TimeCount = 0;
            SDDevManageCtrl(EN_DEV_BEEP, EN_BEEP_OFF, 0, NULL);
            BEEPON = 2;
        }
    }
    else if(BEEPON == 2)
    {
        if(TimeCount ++ == 5)
        {
            TimeCount = 0;
            SDDevManageCtrl(EN_DEV_BEEP, EN_BEEP_ON, 0, NULL);
            BEEPON = 3;
        }
    }
    else if(BEEPON == 3)
    {
        if(TimeCount ++ == 10)
        {
            TimeCount = 0;
            SDDevManageCtrl(EN_DEV_BEEP, EN_BEEP_OFF, 0, NULL);
            BEEPON = 4;
        }
    }
    else if(BEEPON == 4)
    {
        if(TimeCount ++ == 20)
        {
            TimeCount = 0;
            BEEPON = 0;
            BeepBlinkCount++;
        }
    }
    if(BeepBlinkCount == 3)
    {
        SDDevManageCtrl(EN_DEV_BEEP, EN_BEEP_OFF, 0, NULL);
        //退出定时器
        os_timer_stop(st_FBL_Ctrl.BeepBlink_timer);
    }
    
}

/**
  * @brief  fbl_TIME_timer_handler
  * @param  None
  * @note	xxx
  * @retval None
  */
static void fbl_TIME_timer_handler(void *param)
{
    static uint8_t TimeDataCnt = 0;
    if(TimeDataCnt ++ == 1)
    {
        //时间计算标志---每一圈就记一次时间
        printf("[TIME=%d]\r\n", st_FBL_Ctrl.TimeData);
        TimeDataCnt = 0;
        if(st_FBL_Ctrl.HilinkCDNModeStart == EN_HILINK_CDN_NULL)
        {
            if(st_FBL_Ctrl.TimeDisplyContinue == true)
            {
                st_FBL_Ctrl.TimeDisplyContinue = false;
                st_FBL_Ctrl.TimeData ++;
                //1s钟发一次时间数据
                PackDataSend(&st_FBL_Ctrl, EN_FBL_HILINK_PROC_TYPE_CNTIME);
            }
        }
        //启动了 CDN模式
        else if(st_FBL_Ctrl.HilinkCDNModeStart == EN_HILINK_CDN_START)
        {
            st_FBL_Ctrl.TimeData = 60;
            st_FBL_Ctrl.HilinkCDNModeStart = EN_HILINK_CDN_RUN;
            //1s钟发一次时间数据
            PackDataSend(&st_FBL_Ctrl, EN_FBL_HILINK_PROC_TYPE_CDNTIME);
        }
        //运行 CDN模式
        else if(st_FBL_Ctrl.HilinkCDNModeStart == EN_HILINK_CDN_RUN)
        {
            st_FBL_Ctrl.TimeData --;
            //倒计时结束
            if(st_FBL_Ctrl.TimeData == 0)
            {
                //1s钟发一次时间数据
                PackDataSend(&st_FBL_Ctrl, EN_FBL_HILINK_PROC_TYPE_CDNTIME);
                //执行BEEP Blink 三次
                os_timer_start(st_FBL_Ctrl.BeepBlink_timer, 10, true);
                //状态设置为停止状态
                st_FBL_Ctrl.HilinkCDNModeStart = EN_HILINK_CDN_STOP;
            }
            //printf("[TIME]");
            //1s钟发一次时间数据
            PackDataSend(&st_FBL_Ctrl, EN_FBL_HILINK_PROC_TYPE_CDNTIME);

            //更新显示
            if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_CNT)
                st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.CNT_Data;
            else if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_TIME)
                st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.TimeData;
            else if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_CAL)
                st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.CaloriesData;
            else if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_CDN)
                st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.CDN_Data;
            //跳绳显示---可以将这个显示放到另外一个任务里执行
            fbl_display_task_send_event();
        }
        //停止 CDN模式
        else if(st_FBL_Ctrl.HilinkCDNModeStart == EN_HILINK_CDN_STOP)
        {
            return;
        }
        //一分钟没有动作自动关机
        if(st_FBL_Ctrl.AutoPowerOffTimeOut ++ >= 60)
        {
            st_FBL_Ctrl.AutoPowerOffTimeOut = 0;
			SD_PowerOff(&stPowerCtrl);//执行自动关机
        }
    }
}


/**
  * @brief  fbl_TIME_timer_handler
  * @param  None
  * @note	xxx
  * @retval None
  */
static void fbl_BCLED_timer_handler(void *param)
{
    pmu_set_led2_value(1); //关闭背光
}

/**
  * @brief  fbl_bat_handle
  * @param  None
	* @note	电池显示处理--读取电池电压写入TM1621C显示
  * @retval None
  */
void fbl_bat_handle(PST_FBL_CTRL pstFBL_Ctrl)
{
    uint8_t local_vbat[2];
    PST_DEV_MANAGE pstADC_DEV = SDDevManageRead(EN_DEV_ADC, ADC_TRANS_SOURCE_VBAT, local_vbat, 0);       
    pstFBL_Ctrl->Vbat = local_vbat[0] | (local_vbat[1] << 8);
    //printf("[VBAT]=%d\r\n", pstFBL_Ctrl->Vbat);
    if(pstFBL_Ctrl->Vbat >= 4000)
        SDDevManageWrite(EN_DEV_TM1621C, EN_BAT_ICON_LEVEL3, NULL, 0);
    else if(pstFBL_Ctrl->Vbat >= 3600)
        SDDevManageWrite(EN_DEV_TM1621C, EN_BAT_ICON_LEVEL2, NULL, 0);
    else if(pstFBL_Ctrl->Vbat >= 3200)
        SDDevManageWrite(EN_DEV_TM1621C, EN_BAT_ICON_LEVEL1, NULL, 0);
    else if(pstFBL_Ctrl->Vbat >= 2800)
        SDDevManageWrite(EN_DEV_TM1621C, EN_BAT_ICON_NULL, NULL, 0);
}

/**
  * @brief  user_task_func
  * @param  None
  * @note	xxx
  * @retval None
  */
static int user_task_func(os_event_t *param)
{
    switch(param->event_id)
    {
        case USER_EVT_BUTTON:
        {
            if(stPowerCtrl.power_on == true)
            {
                button_msg = (struct button_msg_t *)param->param;
                //co_printf("KEY 0x%08x, TYPE %s.\r\n", button_msg->button_index, button_type_str[button_msg->button_type]);
                //先设置每次按键操作下读取一次电池电压
                fbl_bat_handle(&st_FBL_Ctrl);
                /* 关机处理---对应键值---对应按下(长按、短按等) */
                if(button_msg->button_index == 0x10000000)
                {
                    if(button_msg->button_type == 0x00)//短按
                        SD_PowerOnRun(); 
                    else if(button_msg->button_type == 0x04)//长按
                    {
                        //清空数据--重发一次时间显示--显示清屏---模式更新
                        //启动定时器打开beep 100ms
                        SDDevManageCtrl(EN_DEV_BEEP, EN_BEEP_ON, 0, NULL);
                        os_timer_start(st_FBL_Ctrl.Beep_timer, st_FBL_Ctrl.Beep_timer_out, false);
                        os_timer_stop(st_FBL_Ctrl.CDN_Delay_timer);
                        //清空数据
                        st_FBL_Ctrl.CNT_Data = 0;
                        st_FBL_Ctrl.TimeData = 0;
                        st_FBL_Ctrl.CaloriesData = 0;
                        st_FBL_Ctrl.CDN_Data = 0;
                        st_FBL_Ctrl.DisplayMode = EN_DISP_MODE_NULL;//显示模式
                        st_FBL_Ctrl.HilinkCDNModeStart = EN_HILINK_CDN_NULL;
                        SD_PowerOnRun();
                        PackDataSend(&st_FBL_Ctrl, EN_FBL_HILINK_PROC_TYPE_CNTIME);
                        PackDataSend(&st_FBL_Ctrl, EN_FBL_HILINK_PROC_TYPE_CNT);
                        printf("[BUTTON RESET]\r\n");
                    }
                }
            }
        }
        break;

        //FBL HiLink CMD
        case USER_EVT_FBLHilinkCMD:
        {
            uint8_t *HiLinkMsg = 0;
            HiLinkMsg = (uint8_t *)param->param;
            switch(*HiLinkMsg)
            {
                case EN_FBL_HILINK_PROC_TYPE_LINK:
                //连接上之后发送一次时间显示
                if(st_FBL_Ctrl.HilinkCDNModeStart == EN_HILINK_CDN_NULL)
                    PackDataSend(&st_FBL_Ctrl, EN_FBL_HILINK_PROC_TYPE_CNTIME);
                printf("[APP CMD][EN_FBL_HILINK_PROC_TYPE_LINK]\r\n");
                break;

                case EN_FBL_HILINK_PROC_TYPE_RESET:
                //清空数据--重发一次时间显示--显示清屏---模式更新
                //启动定时器打开beep 100ms
                SDDevManageCtrl(EN_DEV_BEEP, EN_BEEP_ON, 0, NULL);
                os_timer_start(st_FBL_Ctrl.Beep_timer, st_FBL_Ctrl.Beep_timer_out, false);
                os_timer_stop(st_FBL_Ctrl.CDN_Delay_timer);
                //清空数据
                st_FBL_Ctrl.CNT_Data = 0;
                st_FBL_Ctrl.TimeData = 0;
                st_FBL_Ctrl.CaloriesData = 0;
                st_FBL_Ctrl.CDN_Data = 0;
                st_FBL_Ctrl.DisplayMode = EN_DISP_MODE_NULL;//显示模式
                st_FBL_Ctrl.HilinkCDNModeStart = EN_HILINK_CDN_NULL;
                SD_PowerOnRun();
                
                PackDataSend(&st_FBL_Ctrl, EN_FBL_HILINK_PROC_TYPE_CNTIME);
                PackDataSend(&st_FBL_Ctrl, EN_FBL_HILINK_PROC_TYPE_CNT);
                printf("[APP CMD][EN_FBL_HILINK_PROC_TYPE_RESET]\r\n");
                break;

                case EN_FBL_HILINK_PROC_TYPE_CDN:
				//倒计时1min
                st_FBL_Ctrl.TimeData = 60;
                st_FBL_Ctrl.CNT_Data = 0;
                st_FBL_Ctrl.CaloriesData = 0;
                st_FBL_Ctrl.CDN_Data = 0;

                //先停止Timer定时器和HALL定时器
                os_timer_stop(st_FBL_Ctrl.HALL_timer);
                os_timer_stop(st_FBL_Ctrl.TIME_timer);

                //修改模式
                st_FBL_Ctrl.HilinkCDNModeStart = EN_HILINK_CDN_READY_START;
                os_timer_start(st_FBL_Ctrl.CDN_Delay_timer, 3000, false);
                PackDataSend(&st_FBL_Ctrl, EN_FBL_HILINK_PROC_TYPE_CDNTIME);
                PackDataSend(&st_FBL_Ctrl, EN_FBL_HILINK_PROC_TYPE_CNT);
                //Beep 响铃一下
                //启动定时器打开beep 100ms
                SDDevManageCtrl(EN_DEV_BEEP, EN_BEEP_ON, 0, NULL);
                os_timer_start(st_FBL_Ctrl.Beep_timer, st_FBL_Ctrl.Beep_timer_out, false);
                printf("[APP CMD][EN_FBL_HILINK_PROC_TYPE_CDN]\r\n");
                break;

                default:
                break;
            }
        }
        break;
    }
    return EVT_CONSUMED;
}


/**
  * @brief  fbl_display_task_send_event
  * @param  None
  * @note	xxx
  * @retval None
  */
void fbl_display_task_send_event(void)
{
    os_event_t fbl_display_event;
    fbl_display_event.event_id = USER_EVT_DISPLAY_TM1621C;
    fbl_display_event.src_task_id = fbl_disply_task_id;
    fbl_display_event.param = NULL;
    fbl_display_event.param_len = 0;
    os_msg_post(fbl_disply_task_id, &fbl_display_event);
}

/**
  * @brief  fbl_display_task_func
  * @param  None
  * @note	xxx
  * @retval None
  */
static int fbl_display_task_func(os_event_t *param)
{
    switch(param->event_id)
    {
        case USER_EVT_DISPLAY_TM1621C:
        {
			SDDevManageCtrl(EN_DEV_TM1621C, st_FBL_Ctrl.DisplayMode, st_FBL_Ctrl.DisplayData, NULL);
        }
        break;
    }
    return EVT_CONSUMED;
}

/**
   * @brief  SD_PowerOnRun
   * @param  None
   * @note	开机运行的内容
   * @retval None
   */
void SD_PowerOff(PST_POWER_CTRL pstPowerCtrl)
{
    //DO Somethings
    pmu_set_led2_value(1);//关闭背光
    SDDevManageCtrl(EN_DEV_RUN_LED, EN_LED_OFF, 0, NULL);//关闭LED
    os_timer_stop(st_FBL_Ctrl.HALL_timer);
    os_timer_stop(st_FBL_Ctrl.TIME_timer);
    pstPowerCtrl->power_on = false;
    st_FBL_Ctrl.HilinkCDNModeStart = EN_HILINK_CDN_NULL;
    printf("[AUTO POWER OFF!]\r\n");

    //清屏
    SDDevManageCtrl(EN_DEV_BEEP, EN_BEEP_OFF, 0, NULL);
    SDDevManageCtrl(EN_DEV_TM1621C, EN_DISP_MODE_NULL, 0, NULL);
    system_power_off(true);
}

/**
   * @brief  SD_PowerOnRun
   * @param  None
   * @note	开机运行的内容
   * @retval None
   */
void SD_PowerOnRun(void)
{
    //清一次自动关机标志
    st_FBL_Ctrl.AutoPowerOffTimeOut = 0;
    //检测电压
    fbl_bat_handle(&st_FBL_Ctrl);
    //显示模式循环
    if(st_FBL_Ctrl.DisplayMode ++ == EN_DISP_MODE_CDN)
        st_FBL_Ctrl.DisplayMode = EN_DISP_MODE_CNT;
    //更新显示
    if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_CNT)
        st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.CNT_Data;
    else if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_TIME)
        st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.TimeData;
    else if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_CAL)
        st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.CaloriesData;
    else if(st_FBL_Ctrl.DisplayMode == EN_DISP_MODE_CDN)
        st_FBL_Ctrl.DisplayData =  st_FBL_Ctrl.CDN_Data;
    #if 0
    SDDevManageCtrl(EN_DEV_TM1621C, st_FBL_Ctrl.DisplayMode, st_FBL_Ctrl.DisplayData, NULL);
    #else
    //切换之前先清屏
    SDDevManageCtrl(EN_DEV_TM1621C, EN_DISP_MODE_NULL, 0, NULL);
    fbl_display_task_send_event();
    #endif
    //打开LED
    SDDevManageCtrl(EN_DEV_RUN_LED, EN_LED_ON, 0, NULL);
    //启动定时器打开beep 100ms
    SDDevManageCtrl(EN_DEV_BEEP, EN_BEEP_ON, 0, NULL);
    os_timer_start(st_FBL_Ctrl.Beep_timer, st_FBL_Ctrl.Beep_timer_out, false);

    pmu_set_led2_value(0); //打开背光
    os_timer_start(st_FBL_Ctrl.LED_timer, st_FBL_Ctrl.LED_timer_out, false);
}


/**
   * @brief  SD_PowerHandle
   * @param  None
   * @note	xxx
   * @retval None
   */
void SD_PowerHandle(PST_POWER_CTRL pstPowerCtrl)
{
    static int PowerOn_Off_Count = 0;
	int K_V = 0;
	//获取左右按键的键值
	K_V = pmu_get_gpio_value(GPIO_PORT_D, GPIO_BIT_4);
	co_printf("[KEY=%d]\r\n", pmu_get_gpio_value(GPIO_PORT_D, GPIO_BIT_4));
	//判断是否有按键一直按下
	if(K_V == 0)
	{
		PowerOn_Off_Count++;
		//开机
		if(pstPowerCtrl->power_on == false)
		{
			if(PowerOn_Off_Count >= pstPowerCtrl->ONKeyTime)
			{
                PowerOn_Off_Count = 0;
                pstPowerCtrl->power_on = true;
				co_printf("[Power on]\r\n");

				/* DO something */
                //启动 timer
                SD_PowerOnRun();
                os_timer_start(st_FBL_Ctrl.HALL_timer, st_FBL_Ctrl.HALL_timer_out, true);
                os_timer_start(st_FBL_Ctrl.TIME_timer, st_FBL_Ctrl.TIME_timer_out, true);
                //发送一次按键任务处理
                /* 关闭定时器 */
				os_timer_stop(&PowerOn_Off_timer);
			}
		}
	}
	else
	{
		//其他的状态就清零
		PowerOn_Off_Count = 0;
		//判断是关机状态下
		if(pstPowerCtrl->power_on == false)
		{
            co_printf("[No Key press Power off]\r\n");
            os_timer_stop(&PowerOn_Off_timer);
            /* DO Something */
            SD_PowerOff(&stPowerCtrl);
            system_power_off(true);
		}
	}
}

/**
   * @brief  PowerOn_Off_time_fn
   * @param  None
   * @note	xxx
   * @retval None
   */
static void PowerOn_Off_time_fn(void *param) 
{
	SD_PowerHandle(&stPowerCtrl);
}

/**
   * @brief  sd_power_timerINIT
   * @param  None
   * @note	xxx
   * @retval None
   */
void sd_power_timerINIT(void)
{
    os_timer_init(stPowerCtrl.power_timer, PowerOn_Off_time_fn, NULL);
    os_timer_start(stPowerCtrl.power_timer, stPowerCtrl.power_timeout, true);
}

/**
  * @brief  SD_APP_032FBL_TaskInit
  * @note  xxx
  * @param  None
  * @retval None
  */
void SD_APP_032FBL_TaskInit(PST_FBL_CTRL pst_fbl_ctrt)
{
    os_timer_init(pst_fbl_ctrt->HALL_timer, fbl_hall_timer_handler, NULL);
    os_timer_init(pst_fbl_ctrt->CDN_Delay_timer, fbl_CDNDealytimer_handler, NULL);
    os_timer_init(pst_fbl_ctrt->Beep_timer, fbl_beep_timer_handler, NULL);
    os_timer_init(pst_fbl_ctrt->BeepBlink_timer, fbl_beepBlink_timer_handler, NULL);
    os_timer_init(pst_fbl_ctrt->TIME_timer, fbl_TIME_timer_handler, NULL);
    os_timer_init(pst_fbl_ctrt->LED_timer, fbl_BCLED_timer_handler, NULL);

    #if 0
    //测试模式
    os_timer_init(&FBL_TEST_TIMER, fbl_test_timer_handler, NULL);
    os_timer_start(&FBL_TEST_TIMER, 50, true);
    #endif

    user_task_id = os_task_create(user_task_func);
    fbl_disply_task_id = os_task_create(fbl_display_task_func);
    //开机定时器
    sd_power_timerINIT();
}
/******************* (C) COPYRIGHT 2011 ShenZhenShiding electronics **************************/
